package com.bill99.golden.inf.sso.client;

import javax.servlet.http.HttpServletRequest;

import com.bill99.golden.inf.sso.http.SsoServiceHttpImpl;
import com.bill99.golden.inf.sso.mdp.dto.CreateTokenRequestDto;
import com.bill99.golden.inf.sso.mdp.dto.DestroyTokenRequestDto;
import com.bill99.golden.inf.sso.mdp.dto.RefreshTokenTimeRequestDto;
import com.bill99.golden.inf.sso.mdp.dto.ValidateTokenRequestDto;
import com.bill99.golden.inf.sso.mdp.dto.ValidateTokenResponseDto;

public class SsoClientHttp implements IssoClient {
	private static String ssoURL;

	public String registerSSO(HttpServletRequest request, String appCode,
			String userId, String userCode, String userName, String orgId,
			String orgCode, String orgName, String departmentId,
			String departmentCode, String departmentName, String memo) {
		String explorerType = request.getHeader("user-agent");
		String userIp = request.getRemoteHost();
		return registerSSO(appCode, userIp, explorerType, userId, userCode,
				userName, orgId, orgCode, orgName, departmentId,
				departmentCode, departmentName, memo);
	}

	public ValidateTokenResponseDto validateSSO(HttpServletRequest request,
			String token, String appCode) {
		// TODO Auto-generated method stub
		String explorerType = request.getHeader("user-agent");
		String userIp = request.getRemoteHost();
		String vUrl = this.getVisitUrl(request, true);
		return validateSSO(appCode, token, userIp, explorerType, vUrl);
	}

	public void destroySSO(HttpServletRequest request, String token,
			String appCode) {
		// TODO Auto-generated method stub
		String explorerType = request.getHeader("user-agent");
		String userIp = request.getRemoteHost();
		destroySSO(appCode, token, userIp, explorerType);
	}

	private String registerSSO(String appCode, String userIp,
			String explorerType, String userId, String userCode,
			String userName, String orgId, String orgCode, String orgName,
			String departmentId, String departmentCode, String departmentName,
			String memo) {
		CreateTokenRequestDto r = new CreateTokenRequestDto();
		r.setAppCode(appCode);
		r.setUserIp(userIp);
		r.setExplorerType(explorerType);
		r.setUserId(userId);
		r.setUserCode(userCode);
		r.setUserName(userName);
		r.setOrgId(orgId);
		r.setOrgCode(orgCode);
		r.setOrgName(orgName);
		r.setDepartmentId(departmentId);
		r.setDepartmentCode(departmentCode);
		r.setDepartmentName(departmentName);
		r.setMemo(memo);
		SsoServiceHttpImpl ssoTokenService = new SsoServiceHttpImpl(
				SsoClientHttp.ssoURL);
		String token = ssoTokenService.createToken(r);
		return token;
	}

	private ValidateTokenResponseDto validateSSO(String appCode, String token,
			String userIp, String explorerType, String visitUrl) {
		ValidateTokenRequestDto r = new ValidateTokenRequestDto();
		r.setAppCode(appCode);
		r.setUserIp(userIp);
		r.setExplorerType(explorerType);
		r.setToken(token);
		r.setVisitUrl(visitUrl);
		r.setValidatePrivilege(true);
		SsoServiceHttpImpl ssoTokenService = new SsoServiceHttpImpl(
				SsoClientHttp.ssoURL);
		ValidateTokenResponseDto response = ssoTokenService.validateToken(r);
		return response;
	}

	private void destroySSO(String appCode, String token, String userIp,
			String explorerType) {
		DestroyTokenRequestDto r = new DestroyTokenRequestDto();
		r.setAppCode(appCode);
		r.setUserIp(userIp);
		r.setExplorerType(explorerType);
		r.setToken(token);
		SsoServiceHttpImpl ssoTokenService = new SsoServiceHttpImpl(
				SsoClientHttp.ssoURL);
		ssoTokenService.destroyToken(r);
	}

	private String getVisitUrl(HttpServletRequest req, boolean checkMethod) {
		String sURI = req.getRequestURI();
		String ctxpath = req.getContextPath();
		if (ctxpath.length() > 0 && ctxpath.charAt(ctxpath.length() - 1) == '/') {
			ctxpath = ctxpath.substring(0, ctxpath.length() - 1);
		}
		if (ctxpath.length() > 0 && sURI.indexOf(ctxpath) == 0) {
			sURI = sURI.substring(ctxpath.length());
		}
		String qstr = req.getQueryString();
		if (qstr != null && qstr.trim().length() > 0) {
			sURI += "?" + qstr;
		}
		if (checkMethod) {
			qstr = req.getQueryString();
			if (qstr != null && qstr.indexOf("method=") >= 0) {
				return sURI;
			}
			String method = req.getParameter("method");
			if (method != null && !method.trim().equals("")) {
				if (sURI.indexOf("?") > 0)
					sURI = sURI + "&method=" + method;
				else
					sURI = sURI + "?method=" + method;
			}
		}
		return sURI;
	}

	public static String getSsoURL() {
		return ssoURL;
	}

	public static void setSsoURL(String ssoURL) {
		SsoClientHttp.ssoURL = ssoURL;
	}

	public void refreshTokenTime(RefreshTokenTimeRequestDto r) {
		// TODO Auto-generated method stub
		SsoServiceHttpImpl ssoTokenService = new SsoServiceHttpImpl(
				SsoClientHttp.ssoURL);
		ssoTokenService.refreshToken(r);
	}

}
